package com.atguigu.hosp.msm.controller;

import com.atguigu.hosp.msm.service.MsmService;
import com.atguigu.yygh.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "短信服务")
@RestController
@RequestMapping("/api/msm")
public class ApiMsmController {
    @Autowired
    private MsmService msmService;
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("发送手机验证码")
    @GetMapping("send/{phone}")
    public Result sendCode(@PathVariable("phone") String phone) {
        // 验证手机从redis中查询验证码,如果有就返回  | key 手机号 value 验证码
        String code = (String) redisTemplate.opsForValue().get(phone);
        if (!StringUtils.isEmpty(code)) {
            return Result.ok();
        }

        // 如果没有调用service方法,整合短信发送服务
          msmService.send(phone);

        return Result.ok();
    }

}
